package com.jprocms.module.cms.service.model;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.object.BeanUtils;
import com.jprocms.module.cms.controller.admin.model.vo.*;
import com.jprocms.module.cms.convert.model.ModelConvert;
import com.jprocms.module.cms.dal.dataobject.model.ModelDO;
import com.jprocms.module.cms.dal.mysql.model.ModelMapper;
import com.jprocms.module.cms.util.CmsContextUtil;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.service.impl.DiffParseFunction;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.MODEL_NOT_EXISTS;
import static com.jprocms.module.cms.enums.LogRecordConstants.*;

/**
 * 模型 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class ModelServiceImpl implements ModelService {

    @Resource
    private ModelMapper modelMapper;

    @Override
    @LogRecord(type = CMS_MODEL_TYPE, subType = CMS_MODEL_CREATE_SUB_TYPE, bizNo = "{{#model.id}}", success = CMS_MODEL_CREATE_SUCCESS)
    @Transactional(rollbackFor = Exception.class)
    public Long createModel(ModelCreateReqVO createReqVO) {
        // 插入
        ModelDO model = ModelConvert.INSTANCE.convert(createReqVO);
        model.setSiteId(CmsContextUtil.getCurrSiteId());
        modelMapper.insert(model);
        // 记录操作日志上下文
        LogRecordContext.putVariable("model", model);
        // 返回
        return model.getId();
    }

    @Override
    @LogRecord(type = CMS_MODEL_TYPE, subType = CMS_MODEL_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}", success = CMS_MODEL_UPDATE_SUCCESS)
    @Transactional(rollbackFor = Exception.class)
    public void updateModel(ModelUpdateReqVO updateReqVO) {
        // 校验存在
        ModelDO oldModel = validateModelExists(updateReqVO.getId());
        // 更新
        ModelDO updateObj = ModelConvert.INSTANCE.convert(updateReqVO);
        modelMapper.updateById(updateObj);
        // 记录操作日志上下文
        LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(oldModel, ModelUpdateReqVO.class));
        LogRecordContext.putVariable("model", updateObj);
    }

    @Override
    @LogRecord(type = CMS_MODEL_TYPE, subType = CMS_MODEL_CONFIG_UPDATE_SUB_TYPE, bizNo = "{{#updateFieldReqVO.id}}", success = CMS_MODEL_CONFIG_UPDATE_SUCCESS)
    @Transactional(rollbackFor = Exception.class)
    public void updateModelField(ModelUpdateFieldReqVO updateFieldReqVO) {
        // 校验存在
        validateModelExists(updateFieldReqVO.getId());
        // 更新
        ModelDO updateObj = ModelConvert.INSTANCE.convert(updateFieldReqVO);
        modelMapper.updateById(updateObj);
        // 记录操作日志上下文
        LogRecordContext.putVariable("model", updateObj);
    }

    @Override
    @LogRecord(type = CMS_MODEL_TYPE, subType = CMS_MODEL_DELETE_SUB_TYPE, bizNo = "{{#id}}", success = CMS_MODEL_DELETE_SUCCESS)
    @Transactional(rollbackFor = Exception.class)
    public void deleteModel(Long id) {
        // 校验存在
        ModelDO modelDO = validateModelExists(id);
        // 删除
        modelMapper.deleteById(id);
        // 记录操作日志上下文
        LogRecordContext.putVariable("model", modelDO);
    }

    private ModelDO validateModelExists(Long id) {
        final ModelDO modelDO = modelMapper.selectById(id);
        if (modelDO == null) {
            throw exception(MODEL_NOT_EXISTS);
        }
        return modelDO;
    }

    @Override
    public ModelDO getModel(Integer id) {
        return modelMapper.selectById(id);
    }

    @Override
    public List<ModelDO> getModelList(Collection<Integer> ids) {
        return modelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ModelDO> getModelPage(ModelPageReqVO pageReqVO) {
        pageReqVO.setSiteId(CmsContextUtil.getCurrSiteId());
        return modelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ModelDO> getModelList(ModelQueryReqVO exportReqVO) {
        exportReqVO.setSiteId(CmsContextUtil.getCurrSiteId());
        return modelMapper.selectList(exportReqVO);
    }

}
